package com.github.edu.repair.manager.service.impl;

import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.orm.serivce.impl.DataRestfulAbstractBaseService;
import com.github.admin.edu.security.jwt.server.entity.TSysRoleStep;
import com.github.edu.repair.api.entity.TMessage;
import com.github.edu.repair.manager.domain.SysRoleStepDomain;
import com.github.edu.repair.manager.service.SysRoleStepServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-30
 */
@Service
@Slf4j
public class SysRoleStepServerServiceImpl extends DataRestfulAbstractBaseService<TSysRoleStep,Long>
        implements SysRoleStepServerService {

    @Autowired
    private SysRoleStepDomain domain;

    @Override
    public String getAllGetEntity(String title, Integer current, Integer rows, String order, String fileId) {
        return null;
    }

    @Override
    public String getAllEntity(String title, String order, String fileId) {
        return null;
    }

    @Override
    public String saveOrUpdate(String jsonEntity) {
        return null;
    }

    @Override
    public String delete(String ids) {
        return null;
    }

    /**
     * 查询用户是否具有指定步骤的权限
     * @param userId 用户表id
     * @param stepId 流程步骤id
     * @return
     */
    @Override
    public String queryRoleStepOauth(String userId, Integer stepId, String roleId, String orgId) {
        //第一步，根据流程步骤，查询该步骤的授权信息
        JsonEntity<TMessage> jsonEntity=new JsonEntity<>();
        TMessage message=new TMessage();
        message.setOauth(false);
        if (null!=stepId){
            List<TSysRoleStep> list=domain.getAllByStepid(stepId);
            if(null!=list&&list.size()>0){
                list.parallelStream().forEach(tSysRoleStep -> {
                    if(StringUtils.isNotBlank(userId)){
                        if((","+tSysRoleStep.getUserid()+",").indexOf(","+userId+",")!=-1){
                            message.setOauth(true);
                            jsonEntity.setData(message);
                            return ;
                        }
                    }
                    if(StringUtils.isNotBlank(roleId)){
                        String[] roles=roleId.split(",");
                        if(null!=roles&&roles.length>0){
                            for (String string:roles){
                                if((","+tSysRoleStep.getRoleid()+",").indexOf(","+string+",")!=-1){
                                    message.setOauth(true);
                                    return;
                                }
                            }
                        }
                    }
                    if(StringUtils.isNotBlank(orgId)){
                        String[] orgs=orgId.split(",");
                        if(null!=orgs&&orgs.length>0){
                            for (String string:orgs){
                                if((","+tSysRoleStep.getOrgid()+",").indexOf(","+string+",")!=-1){
                                    message.setOauth(true);
                                    return;
                                }
                            }
                        }
                    }
                });
            }
        }
        jsonEntity.setData(message);
        return JsonUtils.toJson(jsonEntity);
    }
}
